package com.ujoku.controller.admin;

import com.labillusion.core.platform.web.pagination.PaginationView;
import com.labillusion.core.platform.web.rest.RESTController;
import com.labillusion.core.util.StringUtils;
import com.ujoku.context.constants.ContextConstants;
import com.ujoku.domain.Member;
import com.ujoku.domain.admin.Parameter;
import com.ujoku.request.admin.MemberListForm;
import com.ujoku.service.MemberService;
import com.ujoku.service.admin.ParametersService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by suggestion on 2015/2/1.
 */
@RestController
public class MemberMngController extends RESTController {

    @Autowired
    private MemberService memberService;

    @Autowired
    private ParametersService parametersService;

    @RequestMapping(value = "/uj-admin/member/list", method = RequestMethod.POST)
    @ResponseBody
    public PaginationView list(@RequestBody @Valid MemberListForm form) {
        Map<String, Object> query = new HashMap<>();
        int startIndex = form.getPageIndex() == 0 ? 0 : form.getPageIndex() * form.getPageSize();
        int pageSize = form.getPageSize();

        query.put("startIndex", startIndex);
        query.put("pageSize", pageSize);
        if (StringUtils.hasText(form.getUser_name()))
            query.put("user_name", form.getUser_name());
        if (StringUtils.hasText(form.getReal_name()))
            query.put("real_name", form.getReal_name());
        if (StringUtils.hasText(form.getPhone_mob()))
            query.put("phone_mob", form.getPhone_mob());
        if (StringUtils.hasText(form.getEmail()))
            query.put("email", form.getEmail());
        if (StringUtils.hasText(form.getLevelName())) {
            Map<String, Object> parameterQuery = new HashMap<>();
            parameterQuery.put("mark", form.getLevelName());
            Parameter parameter = (Parameter) parametersService.selectOne(parameterQuery);
            if (parameter != null) {
                query.put("level", Integer.parseInt(parameter.getKey().substring(5)));
            }
        }

        PaginationView<Member> view = new PaginationView<>();

        List<Member> list = memberService.selectList(query);

        for(Member m : list){
            setLevelName(m);
        }

        view.setList(list);
        int count = memberService.selectCount(query);
        view.setCount(count);

        return view;

    }

    private void setLevelName(Member member) {
        Parameter p = parametersService.getParameterByKey(ContextConstants.MEMBER_LEVEL + member.getLevel());
        if (p != null) {
            member.setLevelName(p.getMark());
            member.setDiscount(Double.parseDouble(p.getValue()));
        }
    }

    @RequestMapping(value = "/uj-admin/member/{id}", method = RequestMethod.GET)
    public Member getById(@PathVariable String id) {

        Member member = (Member)memberService.selectById(id);
        setLevelName(member);

        return member;
    }


}
